//
//  LZSearchRestaurantListViewController.m
//  laziz_Consumer
//
//  Created by xbn_iMac on 2016/12/22.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZSearchRestaurantListViewController.h"
#import "LZSearchRequestPresenter.h"
#import "LZMapLocationManager.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "UITableView+LZTableView.h"
#import "LZRestaurantTableViewCell.h"
#import "LAMerchantDetailViewController.h"
#import "LZRestaurantListModel.h"
#import "LZNoDataPublicView.h"
#import "MJRefresh.h"

NSString *const kSearchRestaurantCellID = @"LZRestaurantTableViewCell";

@interface LZSearchRestaurantListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *table;
@property(nonatomic, strong) NSMutableArray *arrData;

@property (nonatomic, strong) LZSearchRequestPresenter *presenter;

@property (nonatomic, assign) int page;
@property (nonatomic, assign) int limit;

@property (nonatomic, assign) NSTimeInterval request_timeStemp;

@end

@implementation LZSearchRestaurantListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.umLogPageViewName = @"搜索列表页面";
    
    [self registCell];
    
    [self configureRefresh];
    
    [self initDataResource];
    
    [self.table.mj_footer beginRefreshing];
    
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma
#pragma mark - private method

- (void)initDataResource {
    self.page = 1;
    self.limit = 10;
    self.request_timeStemp = 0;
    self.arrData = [NSMutableArray array];
    self.presenter = [[LZSearchRequestPresenter alloc] init];
}

- (void)registCell {
    self.table.separatorColor = [UIColor clearColor];
    [self.table registerNib:[UINib nibWithNibName:kSearchRestaurantCellID bundle:nil] forCellReuseIdentifier:kSearchRestaurantCellID];
}

- (void)configureRefresh
{
    self.table.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upLoadMoreRestaurantListData)];
    [self.table configFooterRefreshText];
}

#pragma
#pragma mark - event

- (void)upLoadMoreRestaurantListData {
    [self requestRestaurantListDataWithPage:self.page andLimit:self.limit];
}

- (void)requestRestaurantListDataWithPage:(int)page andLimit:(int)limit {
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    
    if ([LZMapLocationManager shareLocationManager].currentLocation) {
        dic[@"mapLocationLongitude"] = [NSNumber numberWithDouble:[LZMapLocationManager shareLocationManager].currentLocation.coordinate.longitude];
        dic[@"mapLocationLatitude"] = [NSNumber numberWithDouble:[LZMapLocationManager shareLocationManager].currentLocation.coordinate.latitude];
    }
    
    dic[@"type"] = self.searchType;
    dic[@"timestamp"] = [NSNumber numberWithLong:self.request_timeStemp];
    
    if ([self.searchType isEqualToString:@"1"]) {
        // 如果type为1的时候（菜系）则"name"值传 id
        dic[@"name"] = self.searchId;
    }
    else {
        dic[@"name"] = self.searchMessage;
    }
    
    WS(weakSelf)
    
    [self.presenter searchRestaurantListWithPrameter:dic andPage:page andLimit:limit onSuccess:^(NSArray *listArr, NSTimeInterval timeStamp) {
        
        [weakSelf.table.mj_footer endRefreshing];
        
        if (listArr) {
            weakSelf.page ++;
            
            if (weakSelf.request_timeStemp <= 0) {
                weakSelf.request_timeStemp = timeStamp;
            }
            
            [weakSelf.arrData addObjectsFromArray:listArr];
            [weakSelf.table reloadData];
            if (listArr.count == 0) {
                if (weakSelf.arrData.count == 0) {
                    [weakSelf.table setNullStringFooterText];
                }
                else {
                    [weakSelf.table setNoMoreDataFooterText];
                }
                weakSelf.table.mj_footer.state = MJRefreshStateNoMoreData;
            }
        }
        
        if (weakSelf.arrData.count == 0) {
            [LZNoDataPublicView showNoDataViewInView:weakSelf.table];
        }
        else {
            [LZNoDataPublicView hiddenNoDataViewInView:weakSelf.table];
        }

    } onFail:^(NSString *fail) {
        [weakSelf.table.mj_footer endRefreshing];
    }];
    
}

#pragma
#pragma mark UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.arrData.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [tableView fd_heightForCellWithIdentifier:kSearchRestaurantCellID cacheByIndexPath:indexPath configuration:^(id cell) {
        
    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LZRestaurantTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kSearchRestaurantCellID];
    cell.type = CellType_Normal;
    cell.isShowDistance = [LZMapLocationManager shareLocationManager].currentLocation?YES:NO;
    if (self.arrData.count > indexPath.row) {
        cell.model = self.arrData[indexPath.row];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    LZRestaurantListModel *model = self.arrData[indexPath.row];
    
    LAMerchantDetailViewController *vc = [[LAMerchantDetailViewController alloc] init];
    vc.restaurantId = [model.restaurantId copy];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}


@end
